12. Java Programming Practice Exercises

035ND C01 L01 A24 JAVA PROGRAMMING INTRO

Java Programming Exercise 1

Task Description:

Complete find the first duplicate character in a string.

Complete the following method so given a string it returns the index of the first duplicated character or -1 if there are no duplicated characters.

Example1: input: abcabc, return: 3

Example2: input: abcd, return: -1

public static int findDuplicate(String input) {
}
Task List:

Java Programming Exercise 2

Task Description:

Complete the “two sum” coding problem.

Given an integer array, return true if two numbers in this array can be summed to target.

Example1: input[1, 2, 3, 4], target:5, return true

Example2: input[1,4,5,1,6], target 12, return false

public static boolean twoSum(int[] nums, int target) {
}
Task List:

Java Programming Exercise 3

Task Description:

Complete the “String reverse” coding problem.

Write a function that takes a string as input and returns the string reversed.

Example1: input: "Hello World!", return "!dlroW olleH"

Example2: input: “abcde”, return “edcba”

public static String reverseString(String s) {
}
Task List:

Java Programming Exercise 4

Task Description:

Complete the “find top k larger number” in an unsorted integer array.

Given an unsorted integer array, return the top k larger number in a sorted list. The k is always less than array size. Make sure the time complexity is less than O(nlogn).

Example 1: [-1, 15, 59, 22, 6, 42, 45, 0], k=4, return {22, 42, 45, 59}

Example 2: [5, 10, 22, 100, 8], k=2, return {22, 100}

Task List:

Java Programming Exercise 5

Task Description:

Complete the “translate number to word” coding exercise.

Given a non-negative integer n, print the number in words.

Example1:

Input: 10245

Output: "ten thousand two hundred forty five"

Example2:

Input: 125

Output: "one hundred twenty five"

public static String translateNumberToWord(int number) {
}
Task List:

Java Programming Practice Exercises - Solutions

035ND C01 L01 A25 JAVA PROGRAMMING EXERCISE